
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>JavaScript setInterval() 函数详解 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="JavaScript概念之screenclientoffsetscrollinneravail的widthleft.html" />
    
    
    <link rel="prev" href="element-ui默认样式修改.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../cmd命令.html">
            
                <a href="../cmd命令.html">
            
                    
                    cmd命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Array.prototype.slice.call.html">
            
                <a href="Array.prototype.slice.call.html">
            
                    
                    Array.prototype.slice.call解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="async 函数.html">
            
                <a href="async 函数.html">
            
                    
                    async 函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="callback&&callback如果存在回调函数就执行.html">
            
                <a href="callback&&callback如果存在回调函数就执行.html">
            
                    
                    callback&&callback() 如果存在回调函数就执行！
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="CSS3 nth-child使用注意.html">
            
                <a href="CSS3 nth-child使用注意.html">
            
                    
                    CSS3 nth-child(n)使用注意
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="css3的 -webkit-min-device-pixel-ratio属性 解决高清的适配.html">
            
                <a href="css3的 -webkit-min-device-pixel-ratio属性 解决高清的适配.html">
            
                    
                    css3的 -webkit-min-device-pixel-ratio属性 解决高清的适配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="CSS文本超出自动缩小字体.html">
            
                <a href="CSS文本超出自动缩小字体.html">
            
                    
                    CSS文本超出自动缩小字体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="element-ui默认样式修改.html">
            
                <a href="element-ui默认样式修改.html">
            
                    
                    element-ui默认样式修改
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10" data-path="JavaScript setInterval函数详解.html">
            
                <a href="JavaScript setInterval函数详解.html">
            
                    
                    JavaScript setInterval() 函数详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="JavaScript概念之screenclientoffsetscrollinneravail的widthleft.html">
            
                <a href="JavaScript概念之screenclientoffsetscrollinneravail的widthleft.html">
            
                    
                    JavaScript概念之screenclientoffsetscrollinneravail的widthleft
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="js获取星期几.html">
            
                <a href="js获取星期几.html">
            
                    
                    js获取星期几
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="js监听滚动条事件和获取浏览器及屏幕高度和宽度等信息的方法.html">
            
                <a href="js监听滚动条事件和获取浏览器及屏幕高度和宽度等信息的方法.html">
            
                    
                    js监听滚动条事件和获取浏览器及屏幕高度和宽度等信息的方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="js时间戳与日期格式之间相互转换.html">
            
                <a href="js时间戳与日期格式之间相互转换.html">
            
                    
                    js时间戳与日期格式之间相互转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="JS数组reduce方法详解及高级技巧.html">
            
                <a href="JS数组reduce方法详解及高级技巧.html">
            
                    
                    JS数组reduce()方法详解及高级技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="js中获取时间new Date详细介绍.html">
            
                <a href="js中获取时间new Date详细介绍.html">
            
                    
                    js中获取时间new Date()详细介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="less传参问题：在url中插入变量今天在需要用less给url传递参数时遇到了小问题，一番搜索后有了下述解决方法.html">
            
                <a href="less传参问题：在url中插入变量今天在需要用less给url传递参数时遇到了小问题，一番搜索后有了下述解决方法.html">
            
                    
                    less传参问题：在url中插入变量今天在需要用less给url传递参数时遇到了小问题，一番搜索后有了下述解决方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="moment 和date-fns.html">
            
                <a href="moment 和date-fns.html">
            
                    
                    moment 和date-fns
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="node.JS中配置http-server.html">
            
                <a href="node.JS中配置http-server.html">
            
                    
                    node.JS中配置http-server
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="Object.keys详解.html">
            
                <a href="Object.keys详解.html">
            
                    
                    Object.keys详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="router-link各种属性解释.html">
            
                <a href="router-link各种属性解释.html">
            
                    
                    router-link各种属性解释
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="stylus使用介绍.html">
            
                <a href="stylus使用介绍.html">
            
                    
                    stylus使用介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="TypeError Cannot read property 'compilation' of undefined.html">
            
                <a href="TypeError Cannot read property 'compilation' of undefined.html">
            
                    
                    TypeError Cannot read property 'compilation' of undefined
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="Vue  keep-alive.html">
            
                <a href="Vue  keep-alive.html">
            
                    
                    Vue  keep-alive
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.25" data-path="Vue - 过渡与动画transition.html">
            
                <a href="Vue - 过渡与动画transition.html">
            
                    
                    Vue - 过渡与动画transition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.26" data-path="vue-axios使用方法.html">
            
                <a href="vue-axios使用方法.html">
            
                    
                    vue-axios使用方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.27" data-path="vue-calendar-component.html">
            
                <a href="vue-calendar-component.html">
            
                    
                    vue-calendar-component
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.28" data-path="vue-infinite-loading.html">
            
                <a href="vue-infinite-loading.html">
            
                    
                    vue-infinite-loading
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29" data-path="vue报错There are multiple modules with names that only differ in casing.html">
            
                <a href="vue报错There are multiple modules with names that only differ in casing.html">
            
                    
                    vue报错There are multiple modules with names that only differ in casing
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30" data-path="Vue中注意target和currentTarget的使用.html">
            
                <a href="Vue中注意target和currentTarget的使用.html">
            
                    
                    Vue中注意target和currentTarget的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.31" data-path="关于给javascript对象添加、删除、修改对象的属性.html">
            
                <a href="关于给javascript对象添加、删除、修改对象的属性.html">
            
                    
                    关于给javascript对象添加、删除、修改对象的属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.32" data-path="获取当前地址坐标.html">
            
                <a href="获取当前地址坐标.html">
            
                    
                    获取当前地址坐标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.33" data-path="解决ajax跨域问题【5种解决方案】.html">
            
                <a href="解决ajax跨域问题【5种解决方案】.html">
            
                    
                    解决ajax跨域问题【5种解决方案】
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.34" data-path="解决vue-cli webpack打包后加载资源的路径问题.html">
            
                <a href="解决vue-cli webpack打包后加载资源的路径问题.html">
            
                    
                    解决vue-cli webpack打包后加载资源的路径问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.35" data-path="时间格式转换：201971转换为2019-7-1.html">
            
                <a href="时间格式转换：201971转换为2019-7-1.html">
            
                    
                    时间格式转换：201971转换为2019-7-1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.36" data-path="数据库密码改了.html">
            
                <a href="数据库密码改了.html">
            
                    
                    数据库密码改了
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.37" data-path="语法getBoundingClientRect.html">
            
                <a href="语法getBoundingClientRect.html">
            
                    
                    语法getBoundingClientRect()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.38" data-path="在js中使用length报错Error in renderTypeErrorcannot read property bgImg of null.html">
            
                <a href="在js中使用length报错Error in renderTypeErrorcannot read property bgImg of null.html">
            
                    
                    在js中使用length报错Error in renderTypeErrorcannot read property bgImg of null
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >JavaScript setInterval() 函数详解</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="javascript-setinterval-&#x51FD;&#x6570;&#x8BE6;&#x89E3;">JavaScript setInterval() &#x51FD;&#x6570;&#x8BE6;&#x89E3;</h2>
<pre><code>                                     448 &#x6B21;&#x6D4F;&#x89C8;                     &#x8BFB;&#x5B8C;&#x9700;&#x8981;&#x2248; 9 &#x5206;&#x949F;                     [JavaScript](https://codeplayer.vip/tag/javascript)                
</code></pre><h4 id="&#x5185;&#x5BB9;&#x76EE;&#x5F55;">&#x5185;&#x5BB9;&#x76EE;&#x5F55;</h4>
<ol>
<li><a href="https://codeplayer.vip/p/j7sfj#&#x8BED;&#x6CD5;" target="_blank">&#x8BED;&#x6CD5;</a></li>
<li><a href="https://codeplayer.vip/p/j7sfj#&#x53C2;&#x6570;" target="_blank">&#x53C2;&#x6570;</a></li>
<li><a href="https://codeplayer.vip/p/j7sfj#&#x8FD4;&#x56DE;&#x503C;" target="_blank">&#x8FD4;&#x56DE;&#x503C;</a></li>
<li><a href="https://codeplayer.vip/p/j7sfj#&#x793A;&#x4F8B;&amp;&#x8BF4;&#x660E;" target="_blank">&#x793A;&#x4F8B;&amp;&#x8BF4;&#x660E;</a></li>
</ol>
<p>JavaScript&#x7684;<code>setInterval()</code>&#x51FD;&#x6570;&#x7528;&#x4E8E;&#x8BBE;&#x5B9A;<em>&#x6BCF;&#x9694;&#x6307;&#x5B9A;&#x7684;&#x65F6;&#x95F4;</em>&#x5C31;&#x6267;&#x884C;&#x5BF9;&#x5E94;&#x7684;&#x51FD;&#x6570;&#x6216;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x8BE5;&#x51FD;&#x6570;&#x5C5E;&#x4E8E;&#x5168;&#x5C40;&#x5BF9;&#x8C61;<code>window</code>&#x3002;</p>
<h3 id="&#xA7;&#x8BED;&#x6CD5;"><a href="https://codeplayer.vip/p/j7sfj#&#x8BED;&#x6CD5;" target="_blank">&#xA7;</a>&#x8BED;&#x6CD5;</h3>
<p><code>setInterval()</code>&#x51FD;&#x6570;&#x7684;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js">setInterval ( code, milliseconds[, args...] )
</code></pre>
<p>&#x590D;&#x5236;</p>
<h3 id="&#xA7;&#x53C2;&#x6570;"><a href="https://codeplayer.vip/p/j7sfj#&#x53C2;&#x6570;" target="_blank">&#xA7;</a>&#x53C2;&#x6570;</h3>
<table>
<thead>
<tr>
<th>&#x53C2;&#x6570;</th>
<th>&#x63CF;&#x8FF0;</th>
</tr>
</thead>
<tbody>
<tr>
<td>code</td>
<td>&#x9700;&#x8981;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x6216;js&#x4EE3;&#x7801;</td>
</tr>
<tr>
<td>milliseconds</td>
<td>&#x6267;&#x884C;&#x51FD;&#x6570;&#x6216;&#x4EE3;&#x7801;&#x7684;&#x95F4;&#x9694;&#x65F6;&#x95F4;&#xFF0C;&#x5355;&#x4F4D;&#x4E3A;&#x6BEB;&#x79D2;</td>
</tr>
<tr>
<td>args</td>
<td>&#x53EF;&#x9009;&#x53C2;&#x6570;&#x7528;&#x4E8E;&#x7ED9;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x53C2;&#x6570;&#xFF0C;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;</td>
</tr>
</tbody>
</table>
<h3 id="&#xA7;&#x8FD4;&#x56DE;&#x503C;"><a href="https://codeplayer.vip/p/j7sfj#&#x8FD4;&#x56DE;&#x503C;" target="_blank">&#xA7;</a>&#x8FD4;&#x56DE;&#x503C;</h3>
<p><code>setInterval()</code>&#x51FD;&#x6570;&#x5C06;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6807;&#x8BC6;&#x7B26; ID&#xFF0C;&#x8FD9;&#x4E2A; ID &#x662F;&#x552F;&#x4E00;&#x7684;(&#x4E00;&#x822C;&#x662F;&#x6574;&#x6570;&#xFF0C;&#x4ECE; 1 &#x5F00;&#x59CB;&#xFF0C;&#x6BCF;&#x8C03;&#x7528;&#x4E00;&#x6B21; setInterval() &#x5C31;&#x52A0; 1)&#x3002;</p>
<p>&#x5982;&#x679C;&#x9700;&#x8981; setInterval() &#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x6216;&#x4EE3;&#x7801;&#x5C1A;&#x672A;&#x6267;&#x884C;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <a href="https://codeplayer.vip/p/j7sfk" target="_blank">clearInterval()</a> &#x51FD;&#x6570;&#x6765;&#x53D6;&#x6D88;&#x8BE5;&#x6267;&#x884C;&#x64CD;&#x4F5C;&#xFF0C;clearInterval() &#x9700;&#x8981;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x5C31;&#x662F; setInterval() &#x8FD4;&#x56DE;&#x7684;&#x6807;&#x8BC6;&#x7B26;ID&#x3002;</p>
<h3 id="&#xA7;&#x793A;&#x4F8B;&#x8BF4;&#x660E;"><a href="https://codeplayer.vip/p/j7sfj#&#x793A;&#x4F8B;&amp;&#x8BF4;&#x660E;" target="_blank">&#xA7;</a>&#x793A;&#x4F8B;&amp;&#x8BF4;&#x660E;</h3>
<p>setInterval() &#x7684;&#x53C2;&#x6570;<code>code</code>&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x662F;&#x4E00;&#x6BB5;js&#x811A;&#x672C;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x6BCF;&#x9694;5&#x79D2;&#x949F;&#x5C31;&#x5F39;&#x51FA;&#x63D0;&#x793A;&#x4FE1;&#x606F;&quot;&#x6B22;&#x8FCE;&#x6765;&#x5230;CodePlayer&quot;&#x3002;</span>
setInterval(<span class="hljs-string">&apos;alert(&quot;&#x6B22;&#x8FCE;&#x6765;&#x5230;CodePlayer&quot;);&apos;</span>, <span class="hljs-number">5000</span>);
</code></pre>
<p>&#x590D;&#x5236;</p>
<p><a href="https://codeplayer.vip/diy.php?f=javascript-setinterval-demo" target="_blank">&#x8FD0;&#x884C;&#x4EE3;&#x7801;</a></p>
<p>&#x53C2;&#x6570;<code>code</code>&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHi</span>(<span class="hljs-params">msg</span>)</span>{
    alert(msg);
}

<span class="hljs-comment">// &#x6BCF;&#x9694;5&#x79D2;&#x949F;&#x5C31;&#x5F39;&#x51FA;&#x63D0;&#x793A;&#x4FE1;&#x606F;&quot;&#x6B22;&#x8FCE;&#x6765;&#x5230;CodePlayer&quot;&#x3002;</span>
setInterval(<span class="hljs-string">&apos;sayHi(&quot;&#x6B22;&#x8FCE;&#x6765;&#x5230;CodePlayer&quot;);&apos;</span>, <span class="hljs-number">5000</span>);
</code></pre>
<p>&#x590D;&#x5236;</p>
<p>&#x5982;&#x679C;&#x53C2;&#x6570;<code>code</code>&#x6267;&#x884C;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5219;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x540D;&#x79F0;&#x6216;&#x8005;&#x533F;&#x540D;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x3002;&#x5982;&#x679C;&#x51FD;&#x6570;&#x9700;&#x8981;&#x53C2;&#x6570;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>setInterval()</code>&#x7684;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x4E3A;&#x5176;&#x4F20;&#x9012;&#x76F8;&#x5E94;&#x7684;&#x53C2;&#x6570;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">//&#x8BE5;&#x51FD;&#x6570;&#x4E0D;&#x9700;&#x8981;&#x53C2;&#x6570;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test</span>(<span class="hljs-params"></span>)</span>{
    alert(<span class="hljs-string">&quot;&#x6D4B;&#x8BD5;&quot;</span>);
}
<span class="hljs-comment">// &#x6BCF;&#x9694;5&#x79D2;&#x949F;&#x5C31;&#x5F39;&#x51FA;&#x63D0;&#x793A;&#x4FE1;&#x606F;&quot;&#x6D4B;&#x8BD5;&quot;&#x3002;</span>
setInterval(test, <span class="hljs-number">5000</span>);

<span class="hljs-comment">// &#x4F7F;&#x7528;&#x533F;&#x540D;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x6BCF;&#x9694;5&#x79D2;&#x949F;&#x5C31;&#x8F93;&#x51FA;&#x63A7;&#x5236;&#x53F0;&#x4FE1;&#x606F;&quot;&#x6D4B;&#x8BD5;2&quot;&#x3002;</span>
setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span> &amp;&amp; <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x6D4B;&#x8BD5;2&quot;</span>);  
}, <span class="hljs-number">5000</span>);

<span class="hljs-comment">// &#x8BE5;&#x51FD;&#x6570;&#x9700;&#x8981;&#x4E24;&#x4E2A;&#x53C2;&#x6570;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params">name, age</span>)</span>{
    alert(<span class="hljs-string">&quot;&#x6211;&#x53EB;&quot;</span> + name + <span class="hljs-string">&quot;&#xFF0C;&#x4ECA;&#x5E74;&quot;</span> + age + <span class="hljs-string">&quot;&#x5C81;!&quot;</span>);
}
<span class="hljs-comment">//&#x6BCF;&#x9694;5&#x79D2;&#x949F;&#x5C31;&#x5F39;&#x51FA;&#x63D0;&#x793A;&#x4FE1;&#x606F;&quot;&#x6211;&#x53EB;CodePlayer&#xFF0C;&#x4ECA;&#x5E74;18&#x5C81;!&quot;&#x3002;</span>
setInterval(sayHello, <span class="hljs-number">5000</span>, <span class="hljs-string">&quot;CodePlayer&quot;</span>, <span class="hljs-number">18</span>);
</code></pre>
<p>&#x590D;&#x5236;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>clearInterval()</code>&#x51FD;&#x6570;&#x6765;&#x53D6;&#x6D88;<code>setInterval()</code>&#x8BBE;&#x5B9A;&#x7684;&#x5B9A;&#x65F6;&#x64CD;&#x4F5C;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-function">function <span class="hljs-title">test</span><span class="hljs-params">()</span></span>{
    alert(<span class="hljs-string">&quot;&#x6D4B;&#x8BD5;&quot;</span>);
}

<span class="hljs-comment">// &#x6BCF;&#x9694;5&#x79D2;&#x949F;&#x5C31;&#x5F39;&#x51FA;&#x63D0;&#x793A;&#x4FE1;&#x606F;&quot;&#x6D4B;&#x8BD5;&quot;&#x3002;</span>
var intervalID = setInterval(test, <span class="hljs-number">5000</span>);

<span class="hljs-comment">// &#x53D6;&#x6D88;&#x8BE5;&#x5B9A;&#x65F6;&#x8BBE;&#x7F6E;</span>
clearInterval(intervalID);
</code></pre>
<p>&#x590D;&#x5236;</p>
<p>&#x6B64;&#x5916;&#xFF0C;&#x8BF7;&#x4E00;&#x5B9A;&#x6CE8;&#x610F;&#xFF1A;<code>setInterval()</code>&#x7684;&#x5B9A;&#x65F6;&#x4EE3;&#x7801;&#x662F;&#x5728;<strong>&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;</strong>&#x4E0B;&#x6267;&#x884C;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x4F60;&#x4E0D;&#x80FD;&#x5728;&#x5B9A;&#x65F6;&#x4EE3;&#x7801;&#x4E2D;&#x8C03;&#x7528;&#x67D0;&#x4E9B;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF0C;&#x5426;&#x5219;&#x53EF;&#x80FD;&#x4F1A;&#x62A5;&#x9519;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x4EE5;&#x4E0B;&#x662F;&#x9519;&#x8BEF;&#x7684;&#x7528;&#x6CD5;&#xFF0C;setInterval&#x7684;&#x5B9A;&#x65F6;&#x4EE3;&#x7801;&#x65E0;&#x6CD5;&#x8C03;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;site</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">var</span> site = <span class="hljs-string">&quot;CodePlayer&quot;</span>;
    setInterval(<span class="hljs-string">&apos;alert(site);&apos;</span>, <span class="hljs-number">5000</span>);   
}
foo();

<span class="hljs-keyword">var</span> x = <span class="hljs-number">15</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">bar</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">var</span> x = <span class="hljs-number">18</span>;
    setInterval(<span class="hljs-string">&apos;alert(x);&apos;</span>, <span class="hljs-number">5000</span>); <span class="hljs-comment">// 15</span>
}
<span class="hljs-comment">// &#x7531;&#x4E8E;setInterval&#x7684;&#x5B9A;&#x65F6;&#x4EE3;&#x7801;&#x662F;&#x5728;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x4E0B;&#x6267;&#x884C;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x8FD9;&#x91CC;&#x7684;x&#x4E3A;15</span>
bar();
</code></pre>
<p>&#x590D;&#x5236;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4EE5;&#x4E0B;&#x65B9;&#x6CD5;&#x6765;&#x95F4;&#x63A5;&#x89E3;&#x51B3;&#x4E0A;&#x8FF0;&#x4F5C;&#x7528;&#x57DF;&#x95EE;&#x9898;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x7701;&#x7565;var&#x4FEE;&#x9970;&#x7B26;&#xFF0C;&#x4F7F;&#x7528;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x4EE3;&#x66FF;&#x5C40;&#x90E8;&#x53D8;&#x91CF;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo1</span>(<span class="hljs-params"></span>)</span>{
    site = <span class="hljs-string">&quot;CodePlayer 1&quot;</span>;
    setInterval(<span class="hljs-string">&apos;alert(site);&apos;</span>, <span class="hljs-number">5000</span>);   
}
foo1();

<span class="hljs-comment">// &#x4E0D;&#x4F7F;&#x7528;&#x53D8;&#x91CF;&#xFF0C;&#x76F4;&#x63A5;&#x4F20;&#x9012;&#x53D8;&#x91CF;&#x7684;&#x503C;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo2</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">var</span> site = <span class="hljs-string">&quot;CodePlayer 2&quot;</span>;
    setInterval(<span class="hljs-string">&apos;alert(&quot;&apos;</span> + site + <span class="hljs-string">&apos;&quot;);&apos;</span>, <span class="hljs-number">5000</span>);
}
foo2();

<span class="hljs-comment">// &#x5EF6;&#x957F;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#xFF0C;&#x4F7F;&#x5176;&#x80FD;&#x591F;&#x8BBF;&#x95EE;&#x5230;&#x8BE5;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo3</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">var</span> site = <span class="hljs-string">&quot;CodePlayer 3&quot;</span>;
    setInterval(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
        alert(site);
    }, <span class="hljs-number">5000</span>);   
}
foo3();

<span class="hljs-comment">// &#x8C03;&#x7528;&#x6307;&#x5B9A;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x7ED9;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x76F8;&#x5E94;&#x7684;&#x53C2;&#x6570;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callback</span>(<span class="hljs-params">msg</span>)</span>{
    alert(msg);
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo4</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">var</span> site = <span class="hljs-string">&quot;CodePlayer 4&quot;</span>;
    setInterval(callback, <span class="hljs-number">5000</span>, site);   
}
foo4();
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="element-ui默认样式修改.html" class="navigation navigation-prev " aria-label="Previous page: element-ui默认样式修改">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="JavaScript概念之screenclientoffsetscrollinneravail的widthleft.html" class="navigation navigation-next " aria-label="Next page: JavaScript概念之screenclientoffsetscrollinneravail的widthleft">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"JavaScript setInterval() 函数详解","level":"1.10","depth":1,"next":{"title":"JavaScript概念之screenclientoffsetscrollinneravail的widthleft","level":"1.11","depth":1,"path":"project/JavaScript概念之screenclientoffsetscrollinneravail的widthleft.md","ref":"project/JavaScript概念之screenclientoffsetscrollinneravail的widthleft.md","articles":[]},"previous":{"title":"element-ui默认样式修改","level":"1.9","depth":1,"path":"project/element-ui默认样式修改.md","ref":"project/element-ui默认样式修改.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"project/JavaScript setInterval函数详解.md","mtime":"2019-06-19T16:50:00.106Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-18T03:07:23.077Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

